package org.springcloud.wisdom.spring_01_canteen.admin;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springcloud.wisdom.spring_01_canteen.admin.send.SmsService;
import org.springcloud.wisdom.spring_01_canteen.common.bean.ResponseData;
import org.springcloud.wisdom.spring_01_canteen.common.controller.BaseController;
import org.springcloud.wisdom.spring_01_canteen.login.dao.Clogindao;
import org.springcloud.wisdom.spring_01_canteen.login.entity.Mall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/can")
public class CanMallController extends BaseController {
    @Autowired
    private CanMallService canMallService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private Clogindao clogindao;
    //查看所有商家
    //分页条件查询
    //管理员审核商家注册
    @RequestMapping("/getPages")
    public ResponseData<Object> getMall(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "mrange",required = false) String mrange,
            @RequestParam(value = "mname",required = false) String mname){
        return returnObject(canMallService.getOnrePage(pageNum,pageSize,mrange,mname));
    }
    //管理员管理商家
    @RequestMapping("/getPages1")
    public ResponseData<Object> getMall1(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "mrange",required = false) String mrange,
            @RequestParam(value = "mname",required = false) String mname){
        return returnObject(canMallService.getOnrePage1(pageNum,pageSize,mrange,mname));
    }

    //查看所有（根据食堂查看）
    @GetMapping("/all")
    public ResponseData<Object> cmall(
            @RequestParam(value = "canteen",required = false) String canteen)
    {
        return returnObject(canMallService.cmall(canteen));
    }

    //修改商家信息
    @PutMapping("/update")
    public ResponseData<Boolean> updateMall(@RequestBody Mall mall){
        return returnBoolean(canMallService.updataMall(mall));
    }
    //修改状态
    //锁定商家状态的端点
    @PutMapping("/lock/{mid}")
    public ResponseData<Boolean> lockMallStatus(@PathVariable Integer mid) {
        boolean result = canMallService.reject(mid);
        return new ResponseData<>(1, "Mall status locked successfully", result);
    }

    //解锁商家状态的端点
    @PutMapping("/unlock/{mid}")
    public ResponseData<Boolean> unlockMallStatus(@PathVariable Integer mid) {
        boolean result = canMallService.approve(mid);
        return new ResponseData<>(1, "Mall status unlocked successfully", result);
    }
    //商家注册审核
    //审核通过的端点
    @GetMapping("/approve")
    public ResponseData<Object> updateMall(@RequestParam("mid") Integer mid) {
        Mall mall = new Mall();
        mall.setMid(mid);
        boolean isUpdated = canMallService.updateMall0(mall);
        if (isUpdated) {
            // 从数据库或其他地方获取商家的手机号
            String phoneNumber = String.valueOf(clogindao.selectBymid(mid));
            System.out.println(phoneNumber);
            try {
                smsService.sendApprovalSms(phoneNumber);
            } catch (Exception e) {
                // 处理短信发送异常
                e.printStackTrace();
            }
        }
        return returnObject(isUpdated);
    }
    //审核不通过
    @GetMapping("/reject")
    public ResponseData<Object> updateMall1(@RequestParam("mid")Integer mid){
        Mall mall=new Mall();
        mall.setMid(mid);
        return  returnObject(canMallService.updateMall1(mall));
    }
}
